package com.honey.mybatis.generator.criteria.method;

import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.criteria.CriteriaConstant;

abstract class BaseMethod extends AbstractMethod {
	
	private static final FullyQualifiedJavaType RETURN_TYPE = new FullyQualifiedJavaType("T"); 
	
	private static final String []ANNOTATION =new String[]{
		//"@SuppressWarnings(\"unchecked\")" 
	};
	
	private static final Parameter[] PARAMETER = new Parameter[] {
		 new Parameter( "F", "field", "数据库字段名称枚举。" ),
		 null,
		 null,
	};

	
	public BaseMethod(){
	}
	public BaseMethod(Clazz clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		return RETURN_TYPE;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getAnnotation()
	 */
	@Override
	protected String[] getAnnotation() {
		return ANNOTATION;
	}
	
	@Override
	protected Parameter[] getParameter() {
		PARAMETER[1] = getFirstParameter();
		PARAMETER[2] = getSecondParameter();
		return PARAMETER;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		CharacterBuilder cb =CharacterBuilderFactory.createDefaultStringBuilder();
		cb.append(CriteriaConstant.ADD_CRITERIA_METHOD).append("(");IndentSpace.newSpace(cb, 3);
		
		cb.append("field,");IndentSpace.newSpace(cb, 3);
		cb.append(CriteriaConstant.TERM_CLASS_NAME).append(".").append(getCondition());
		Parameter parameter = getFirstParameter();
		if(  parameter != null){
			cb.append(',');IndentSpace.newSpace(cb, 3);
			cb.append(parameter.getName());
			parameter = getSecondParameter();
			if(parameter != null){
				cb.append(",");
				IndentSpace.newSpace(cb, 3);
				cb.append(parameter.getName());
			}
		}
		IndentSpace.newSpace(cb, 2);
		cb.append(")");
		method.addBodyLine(cb.toString());
		method.addBodyLine("return (T) this ");
	}
	
	protected abstract String getCondition() ;
	
	/**
	 * 第一个参数必须传入
	 * @return
	 */
	protected abstract Parameter getFirstParameter() ;
	
	/**
	 * 第二个参数非必须传入
	 * @return
	 */
	protected Parameter getSecondParameter() {
		return null;
	}
	@Override
	protected String getMethodName() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	protected boolean isBaseType() {
		
		return super.isBaseType();
	}
	
	
	
	
}
